﻿using System;
using Caliburn.Micro;
using MediaOwl.Common;
using MediaOwl.WebSearch.BingService;

namespace MediaOwl.WebSearch.Data
{
    public class VideoSearch : IResult
    {
        private readonly string searchTerm;
        private readonly uint itemsPerPage;
        private readonly uint offset;
        private readonly string[] filters;
        private readonly Action<VideoResponse> onSuccess;
        private readonly Action<Exception> onFail;

        public VideoSearch(string searchTerm, uint itemsPerPage, uint offset, Action<VideoResponse> onSuccess, Action<Exception> onFail = null)
        {
            this.searchTerm = searchTerm;
            this.itemsPerPage = itemsPerPage;
            this.offset = offset;
            this.onSuccess = onSuccess;
            this.onFail = onFail;
        }

        public VideoSearch(string searchTerm, uint itemsPerPage, uint offset, string[] filters, Action<VideoResponse> onSuccess, Action<Exception> onFail = null)
        {
            this.searchTerm = searchTerm;
            this.itemsPerPage = itemsPerPage;
            this.offset = offset;
            this.filters = filters;
            this.onSuccess = onSuccess;
            this.onFail = onFail;
        }

        public void Execute(ActionExecutionContext context)
        {
            ViewHelper.SetScroll(context, "Results");

            var request = new SearchRequest
                              {
                                  AppId = BingDataAccess.AppId,
                                  Version = BingDataAccess.Version,
                                  Adult = AdultOption.Off,
                                  AdultSpecified = true,
                                  Query = searchTerm,
                                  Sources = new[] {SourceType.Video},
                                  Video =  new VideoRequest
                                               {
                                                   Count = itemsPerPage, 
                                                   CountSpecified = true, 
                                                   Offset = offset, 
                                                   OffsetSpecified = true,
                                                   Filters = filters
                                               }
                              };

            var port = new BingPortTypeClient();
            port.SearchCompleted += (s, e) =>
                                        {
                                            if (e.Error == null)
                                            {
                                                if (onSuccess != null)
                                                    Caliburn.Micro.Execute.OnUIThread(() => onSuccess(e.Result.Video));
                                            }
                                            else
                                            {
                                                if (onFail != null)
                                                    Caliburn.Micro.Execute.OnUIThread(() => onFail(e.Error));
                                            }
                                            Completed(this, new ResultCompletionEventArgs {Error = e.Error});
                                        };
            port.SearchAsync(request);
        }

        public event EventHandler<ResultCompletionEventArgs> Completed = delegate { };
    }
}